﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
using DevExpress.XtraEditors;


using Common;
using DataBusiness;

namespace UI
{
    public partial class frmLogin : DevExpress.XtraEditors.XtraForm
    {
        public frmLogin()
        {
            InitializeComponent();
        }



        //登录
        private void frmLogin_Load(object sender, EventArgs e)
        {
            string userAccount = string.Empty;
            string userPwd = string.Empty;

            try
            {
                RegistryKey regKey;
                regKey = Registry.CurrentUser.OpenSubKey(AppPara.Account_RegistryKey);

                if (regKey != null)
                {
                    if (regKey.GetValue("UserAccount") != null)
                        userAccount = (string)regKey.GetValue("UserAccount");
                    if (regKey.GetValue("Password") != null)
                        userPwd = (string)regKey.GetValue("Password");
                    regKey.Close();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }

            if (userAccount != string.Empty)
            {
                txtAccount.Text = userAccount;
                txtPwd.Text = userPwd;
                chkRem.Checked = true;
            }
            else
            {
                txtAccount.Focus();
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtAccount.Text))
            {
                AppMethod.ShowWarning("请输入帐号");
                txtAccount.Focus();
                return;
            }
            else if (string.IsNullOrEmpty(txtPwd.Text))
            {
                AppMethod.ShowWarning("请输入密码");
                txtPwd.Focus();
                return;
            }
            string s = AppMethod.Encrypt(txtPwd.Text);
            string iRet = UserBLL.UserLogin(txtAccount.Text, txtPwd.Text);
            if (iRet == "-1" || iRet == "-2" || iRet == "-3")
            {
                AppMethod.ShowTips("账号或密码错误");
                txtAccount.Focus();
                return;
            }
            else//登录成功
            {
                // 记住密码
                try
                {
                    RegistryKey regKey;
                    regKey = Registry.CurrentUser.CreateSubKey(AppPara.Account_RegistryKey);
                    if (chkRem.Checked)
                    {
                        regKey.SetValue("UserAccount", txtAccount.Text);
                        regKey.SetValue("Password", txtPwd.Text);
                    }
                    else
                    {
                        regKey.DeleteValue("UserAccount", false);
                        regKey.DeleteValue("Password", false);
                    }
                    regKey.Close();
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
                // 全局变量记录账号
                AppPara.Global_User_Account = txtAccount.Text;
                // 全局变量记录用户ID
                AppPara.Global_User_Id = iRet.Split('|')[1];
                // 全局变量记录姓名
                AppPara.Global_User_Name = iRet.Split('|')[2];
            }
            //隐藏登录窗体
            this.Hide();
            //打开主窗体
            frmMain frmMains = new frmMain();
            frmMains.Show();
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();

        }

        private void txtPwd_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 0x0d)    // 回车Enter
            {
                btnLogin_Click(null, null);
            }
        }
    }
}
